package com.lzy.cloud.base.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.ProtocolDto;
import com.lzy.cloud.base.dto.ShopProtocolDto;
import com.lzy.cloud.base.request.ShopProtocolRequest;
import com.lzy.cloud.base.service.ProtocolService;
import com.lzy.cloud.base.service.ShopProtocolService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商家协议关联
 *
 * @author lc
 * @version 2018/8/17
 */
@Component
public class ShopProtocolFacade {

    @Autowired
    private ShopProtocolService shopProtocolService;

    @Autowired
    private ProtocolService protocolService;

    /**
     * 增
     * @param request
     * @return
     */
    public Boolean create(ShopProtocolRequest request) {
        ShopProtocolDto dto = DozerUtils.map(request, ShopProtocolDto.class);
        for (Long protocolId:request.getProtocolIds()){
            dto.setProtocolId(protocolId);
            ProtocolDto protocolDto = protocolService.find(dto.getProtocolId());
            Ensure.that(protocolDto).isNotNull("00030002");
            Long id = shopProtocolService.create(dto);
            if (id < 0 || id == 0){
                return false;
            }
        }
        return true;
    }

    /**
     * 分页查询
     * @param pager
     * @param map
     * @return
     */
    public PageInfo<ShopProtocolDto> selectList(Pager pager, ShopProtocolDto map) {
        return shopProtocolService.selectList(pager,map);
    }

    /**
     * 编辑
     * @param request
     * @return
     */
    public Boolean update(ShopProtocolRequest request) {
        //删除对应shopId的协议
        shopProtocolService.deleteByShopId(request.getShopId());
        return create(request);
    }
}
